package com.jshoperxms.controller.api.langdu;

import com.jshoperxms.Response;
import com.jshoperxms.controller.utils.enums.BaseEnums;
import com.jshoperxms.controller.utils.statickey.TagWords;
import com.jshoperxms.entity.YdArticleRecordRdT;
import com.jshoperxms.entity.YdRecordRdT;
import com.jshoperxms.service.YdArticleRecordRdTService;
import com.jshoperxms.service.YdRecordRdTService;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by sdywcd on 2018/11/12.
 * Des:
 */
@RestController
@RequestMapping("/api/public/index")
public class YdPublicIndexController {


    @Resource
    private YdRecordRdTService ydRecordRdTService;

    @Resource
    private YdArticleRecordRdTService ydArticleRecordRdTService;

    /**
     * 获取推荐的朗读内容
     * @return
     */
    @RequestMapping(value="/findRecommendResults",method= RequestMethod.POST)
    public ResponseEntity findRecommendResults(){
        Map<String,Object> responsedata=new HashMap<>();
        Criterion criterion= Restrictions.and(Restrictions.eq("recommend","1")).add(Restrictions.eq("ispublic","1")).add(Restrictions.eq(TagWords.STATUS, BaseEnums.DataUsingState.USING.getState()));
        List<YdRecordRdT>list=this.ydRecordRdTService.findByCriteriaByPage(YdRecordRdT.class,criterion, Order.desc(TagWords.UPDATETIME),1,5);
        Collections.shuffle(list,new Random());
        responsedata.put("results",list);
        responsedata.put(TagWords.SUCCESS,true);
        return Response.success(responsedata);
    }

    /**
     * 获取每日推荐的图文内容
     * @return
     */
    @RequestMapping(value="/findDailyRecommendArtRecord",method= RequestMethod.POST)
    public ResponseEntity findDailyRecommendArtRecord(){
        Map<String,Object> responsedata=new HashMap<>();
        Criterion criterion= Restrictions.and(Restrictions.eq("recommend","1")).add(Restrictions.eq("ispublic","1")).add(Restrictions.eq(TagWords.STATUS, BaseEnums.DataUsingState.USING.getState()));
        List<YdArticleRecordRdT> bean=this.ydArticleRecordRdTService.findByCriteriaByPage(YdArticleRecordRdT.class,criterion,Order.desc(TagWords.UPDATETIME),1,4);
        responsedata.put(TagWords.BEANLIST,bean);
        responsedata.put(TagWords.SUCCESS,true);
        return Response.success(responsedata);

    }
}
